import { createRouter, createWebHistory } from 'vue-router'

// 主布局
const MainLayout = () => import('@/layouts/MainLayout.vue')

// 数据库服务相关页面
const DatabaseService = () => import('@/views/database/DatabaseService.vue')
// 导入视图已改为对话框，不再需要路由
// const ImportView = () => import('@/views/database/ImportView.vue')

// 其他服务页面
const FileService = () => import('@/views/file/FileService.vue')
const ApiService = () => import('@/views/api/ApiService.vue')
const SettingsService = () => import('@/views/settings/SettingsService.vue')

// 测试页面 - 已移除
// const VTableTest = () => import('@/components/test/VTableTest.vue')

const routes = [
  {
    path: '/',
    component: MainLayout,
    redirect: '/database',
    children: [
      {
        path: 'database',
        name: 'DatabaseService',
        component: DatabaseService,
        meta: {
          title: '数据库服务'
        }
      },
      // 导入页面已改为对话框模式，不再需要此路由
      // {
      //   path: 'database/import',
      //   name: 'ImportView',
      //   component: ImportView,
      //   meta: {
      //     title: '导入数据'
      //   }
      // },
      {
        path: 'file',
        name: 'FileService',
        component: FileService,
        meta: {
          title: '文件服务'
        }
      },
      {
        path: 'api',
        name: 'ApiService',
        component: ApiService,
        meta: {
          title: 'API服务'
        }
      },
      {
        path: 'settings',
        name: 'SettingsService',
        component: SettingsService,
        meta: {
          title: '设置'
        }
      },
      // 测试路由已移除
      // {
      //   path: 'test',
      //   name: 'Test',
      //   children: [
      //     {
      //       path: 'vtable',
      //       name: 'VTableTest',
      //       component: VTableTest,
      //       meta: {
      //         title: 'VTable测试'
      //       }
      //     }
      //   ]
      // }
    ]
  }
  // 删除独立的导入页面路由
  // {
  //   path: '/import',
  //   name: 'ImportView',
  //   component: ImportView,
  //   meta: {
  //     title: '导入数据'
  //   }
  // }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - Database Manager`
  } else {
    document.title = 'Database Manager'
  }
  
  // 检查服务是否可用
  if (to.meta.disabled) {
    console.warn(`${to.meta.title}功能暂未开放`)
    next(false)
    return
  }
  
  next()
})

router.afterEach((to, from) => {
  // 路由切换后的处理
  console.log(`Route changed from ${from.path} to ${to.path}`)
})

export default router 